//
//  PopoverAnimator.swift
//  CXWeibo
//
//  Created by chenxu on 16/9/18.
//  Copyright © 2016年 陈旭. All rights reserved.
//

import UIKit

class PopoverAnimator: NSObject {
    // MARK:- 对外属性
    var presentedFrame : CGRect = CGRectZero
    var isPresented : Bool = false

    var callBack : ((isPresented : Bool)->())?
    
    // MARK:- 自定义构造函数
    init(callBack : (isPresented : Bool)->()) {
        self.callBack = callBack
    }
}
// MARK:- 自定义转场代理的方法
extension PopoverAnimator : UIViewControllerTransitioningDelegate{
    // 改变弹出view的尺寸
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        let presentation = CXPresentationController(presentedViewController: presented,
            presentingViewController: presenting)
        presentation.presentedFrame = presentedFrame
        return presentation
    }
    // 自定义弹出动画
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        callBack!(isPresented: isPresented)
        return self
    }
    // 自定义退出动画
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        callBack!(isPresented: isPresented)
        return self
    }
}

// MARK:- 自定义弹出动画代理方法
extension PopoverAnimator : UIViewControllerAnimatedTransitioning{
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? presentedAnimation(transitionContext) : dismissedAnimation(transitionContext)
    }
    func presentedAnimation(transitionContext: UIViewControllerContextTransitioning){
        // UITransitionContextFromViewKey, and UITransitionContextToViewKey
        // 拿到要弹出的view
        let presentedView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        // 将弹出的view添加到containerView中
        transitionContext.containerView()?.addSubview(presentedView)
        // 执行动画
        presentedView.transform = CGAffineTransformMakeScale(1.0, 0.0)
        // 设置锚点
        presentedView.layer.anchorPoint = CGPointMake(0.5, 0.0)
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            presentedView.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                // 必须告诉上下文你已经完成动画
                transitionContext.completeTransition(true)
        }
        
    }
    func dismissedAnimation(transitionContext: UIViewControllerContextTransitioning){
        // UITransitionContextFromViewKey, and UITransitionContextToViewKey
        // 拿到要消失的view
        let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        // 执行动画、
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            dismissView?.transform = CGAffineTransformMakeScale(1.0, 0.00001)// 设置为0不行，系统bug
            }) { (_) -> Void in
                dismissView?.removeFromSuperview()
                // 必须告诉上下文你已经完成动画
                transitionContext.completeTransition(true)
        }
    }
}